﻿#include"XQPushServer.h"
#include"XQPushReply.h"
#include"XQHttpReplyAccount.h"
#include"XQMySql.hpp"
#include"XQLog.hpp"
#include"XQPush.h"
#include"XQMail.h"
#define PushServerLog XQLog(this->logName()) //http服务器日志
XQPushServer::XQPushServer(QObject* parent)
	:XQHttpServer(parent)
{
	init();
}

XQMySql* XQPushServer::userMysql() const
{
	return m_userMysql;
}

XQMySql* XQPushServer::appMysql() const
{
	return m_appMysql;
}

XQMail* XQPushServer::mail() const
{
	return m_mail;
}

QString XQPushServer::name() const
{
	return m_name;
}

void XQPushServer::userMysqlBridgingToAppMysql()
{
	if (userMysql() == appMysql())
		return;
	m_userMysql->deleteLater();
	m_userMysql = m_appMysql;
	if(m_account)
		m_account->setUserMysql(m_userMysql);
}

void XQPushServer::userMysql_Create()
{
	if (userMysql() != appMysql())
		return;
	m_userMysql = new XQMySql(this);
	//connect(m_userMysql, &XQMySql::connectionSucceed, this, &XQPushServer::userMysql_init);
	if (m_account)
		m_account->setUserMysql(m_userMysql);
}

void XQPushServer::setName(const QString& name)
{
	if (name.isEmpty())
		return;
	m_name = name;
	modifyLogName(name);
	if(m_mail)
		m_mail->setSenderName(name);
}


void XQPushServer::init()
{
	userMysql_Create();
	m_appMysql = new XQMySql(this);
	//setTimeoutConnection(5_s);
	setModel(NetworkType::Tcp);
	mail_init();
	account_init();
	push_init();
	setName("QPushMessage");
}

void XQPushServer::mail_init()
{
	m_mail = new XQMail(this);
	//邮箱smtp,如"smtp.126.com"
	m_mail->setServerName("smtp.qq.com");
	//邮箱端口号，163端口25，qq端口587
	m_mail->setServerPort(587);

	
}

void XQPushServer::account_init()
{
	m_account = new XQHttpReplyAccount(this);
	m_account->bindLogName(m_pLogName);
	m_account->bindDebugModels(m_pDebug);
	m_account->bindName(&m_name);
	m_account->setUserMysql(m_userMysql);
	m_account->setMail(m_mail);
	addReplyModel(m_account);
}

void XQPushServer::push_init()
{
	auto m_push = new XQPushReply(this);
	m_push->bindLogName(m_pLogName);
	m_push->bindDebugModels(m_pDebug);
	m_push->bindName(&m_name);
	m_push->setAccountManagement(m_account);
	m_push->setAppMysql(m_appMysql);
	m_push->setMail(m_mail);
	addReplyModel(m_push);
}

